import {getToken,removeToken} from './auth';
import env from './env';
function service(options = {}) {
	if(options.url){
		let pre = options.url.substring(0,4);
		if(pre === 'http'){
			options.url = options.url;
		}else{
			options.url = `${env.baseUrl}${options.url}`;
		}
	}
	
	// 判断本地是否存在token，如果存在则带上请求头
	let appVersion = uni.getSystemInfoSync().appVersion;
	console.log("[network]请求参数:",(options||{}));
	options.header = {
		'Content-Type': 'application/json; charset=UTF-8', // 默认值
		'Source-Type': 'APP',
		// #ifdef APP-PLUS
		'terminal':'APP',
		// #endif
		// #ifdef MP 
		'terminal':'WECHAT_APPLET',
		// #endif
		// #ifdef H5
		'terminal':'WEB',
		// #endif
		'Accept': '*/*',
		'lanlinker-version': appVersion || '3.0.0',
		'lanlinker-satoken': `${getToken()}`,
		'Referer': 'https://www.gyhapp.com'
	};

	console.error("小程序token:",getToken());
	return new Promise((resolve, rejected) => {
		options.success = (res) => {
			let mes = '请求异常';
			if(res && res.data && res.data.code === '00000'){
				resolve(res);
			}else{
				rejected(res);
			}
		};
		options.fail = (err) => {
			// 请求失败弹窗
			uni.showToast({
				icon: 'none',
				duration: 2000,
				title: '服务器错误,请稍后再试['+(JSON.stringify(err))+']'
			});
			console.log("[network]请求失败:",(res||{}),options);
			rejected(err);
		};
		uni.request(options);
	});
}

export default service;
